//
//  CertPublicKeyInfo.m
//  TestProject
//
//  Created by 毕武侠 on 2022/2/19.
//  Copyright © 2022 zhulong. All rights reserved.
//

#import "CertPublicKeyInfo.h"
#import "CertTLV.h"
#import "CertOID.h"

@implementation CertPublicKeyInfo

/*
 解析：主题公钥算法
 30 59(89)
    30 13(19):
        06 07: 2A 86 48 CE 3D 02 01
        06 08: 2A 81 1C CF 55 01 82 2D
    03 42(66): 00 04 C9 B4 EE 4D ....
 解析：主题公钥算法 一证通
 30 81 9F(159):
    30 0D(13):
        06 09:(09) 2A 86 48 86 F7 0D 01 01 01
        05 00
    03 81 8D(141): 00 30 81 89 02 81 81 00 ....
 */
- (NSDictionary<NSNumber *,NSString *> *)objectType {
    return @{
        @(0x30) : @"algorithm",
        @(0x03) : @"subjectPublicKey"
    };
}

- (id)getObject:(NSData *)data type:(uint16_t)type {
    if (type == 0x30) {
        return [self getList:data type:type];
    } else if (type == 0x130) {
        char *sdata = (char *)data.bytes;
        return [CertTLV value:sdata begin:0];
    } else if (type == 0x03) {
        char *sdata = (char *)data.bytes;
        return [CertTLV value:sdata begin:0];
    } else {
        return [super getObject:data type:type];
    }
}

- (NSString *)description {
    NSMutableString *str = [NSMutableString string];
    
    NSMutableString *algorithms = [NSMutableString string];
    for (int i = 0; i < self.algorithm.count; i++) {
        if (self.algorithm[i].length > 0) {
            [algorithms appendFormat:@"%@(%@), ", self.algorithm[i], [CertOID oid: self.algorithm[i]]];
        }
    }
    if (algorithms.length > 0) {
        [algorithms deleteCharactersInRange:NSMakeRange(algorithms.length-2, 2)];
        [str appendFormat:@"公钥算法: [%@]\n", algorithms];
    }
    if (self.subjectPublicKey.length > 0) {
        [str appendFormat:@"公钥数据: [%@]\n", self.subjectPublicKey];
    }
    
    if (str.length > 0) {
        [str deleteCharactersInRange:NSMakeRange(str.length-1, 1)];
    }
    return str;
}

@end
